const request = require('../../utils/request')
Page({

    /**
     * 页面的初始数据
     */
    data: {
        moneyTypeList: [],
        active: '',
        money: 0,
        moneyDesc: "",
        accountMoney: 0,
        recharges: []
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.getMoneyType()
        this.getUserById()
    },
    handleClick(e) {
        console.log(e);
        const {
            value,
            money,
            desc
        } = e.currentTarget.dataset

        this.setData({
            active: value,
            money,
            moneyDesc: desc
        })
        console.log(this.data.money, this.data.active, this.data.moneyDesc);
    },
    getMoneyType() {
        const that = this
        request({
            url: '/moneyType',
            data: {
                currPage: 1,
                pageSize: 50,
            },
            success(data) {
                console.log('请求成功：', data);
                that.setData({
                    moneyTypeList: data.data.records,
                    active: data.data.records[0].id,
                    money: data.data.records[0].rechargeAmount,
                    moneyDesc: "充" + data.data.records[0].rechargeAmount + "送" + data.data.records[0].giftAmount,
                })
            },
            fail(err) {
                console.log('请求失败：', err);
            }
        })
    },
    handlePayment() {
        const token = wx.getStorageSync('token')
        if (!token) return wx.showToast({
            icon: "none",
            title: '请先登录在进行充值',
        })
        const userinfo = JSON.parse(wx.getStorageSync('userinfo'))
        if (!userinfo.mobile) return wx.showToast({
            icon: "none",
            title: '请先完善信息在进行充值',
        })
        let that = this
        request({
            url: `/wxRecharge/${userinfo.openId}`,
            method: 'post',
            data: {
                moneyDesc: that.data.moneyDesc,
                rechargeAmount: that.data.money
                // rechargeAmount: 0.01
            },
            success: (res) => {
                wx.requestPayment({
                    "timeStamp": res.data.timeStamp,
                    "nonceStr": res.data.nonceStr,
                    "package": res.data.package,
                    "signType": "RSA",
                    "paySign": res.data.paySign,
                    "success": function (res) {
                        console.log("支付成功", res)
                        wx.showToast({
                            title: '支付成功',
                        })
                        that.getUserById()
                    },
                    "fail": function (res) {
                        console.log("支付失败", res)
                        wx.showToast({
                            icon: "error",
                            title: '支付失败',
                        })
                    },
                    "complete": function (res) {
                        console.log("支付完成", res)
                    }
                })
            },
            fail: (err) => {
                console.log('充值请求失败', err);
            }
        })
    },
    getUserById() {
        const id = JSON.parse(wx.getStorageSync("userinfo")).id
        if (!id) return
        const that = this
        request({
            url: `/user/${id}`,
            success: (res) => {
                console.log('获取成功', res);
                that.setData({
                    accountMoney: res.data.money
                })
                wx.setStorageSync('userinfo', JSON.stringify(res.data))
            },
            fail: (err) => {
                console.log('获取失败', err);
            }
        })
    },
    toPage(){
        wx.navigateTo({
          url: '/pages/rechargeRecords/rechargeRecords',
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})